package com.asion.vmovie.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.DetialsBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asion on 16-10-19.
 */

public class PingDaoDetailAdapter extends RecyclerView.Adapter <PingDaoDetailAdapter.Holder>{

    private Context context;
    private List<DetialsBean.DataBean> list;
    private NewsViewAdapter.OnItemClickListener onItemClickListener;

    public PingDaoDetailAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setOnItemClickListener(NewsViewAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.news_item, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {

        DetialsBean.DataBean dataBean = list.get(position);
        Glide.with(context).load(dataBean.getImage()).asBitmap()
                .centerCrop()
                .animate(R.anim.zoom_in).error(R.mipmap.default_error)
                .into(holder.newsImage);
        holder.newsTitle.setText(dataBean.getTitle());
        holder.newsSubtitle.setText(dataBean.getCates().get(0).getCatename() +"  / "
                +Integer.parseInt(dataBean.getDuration())/60+"'"+Integer.parseInt(dataBean.getDuration())%60+"\"");
        holder.itemView.setOnClickListener(v->onItemClickListener.onItemClick(dataBean));
    }

    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }

    public void removeAll() {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class Holder extends RecyclerView.ViewHolder{

        @BindView(R.id.news_image)
        ImageView newsImage;
        @BindView(R.id.news_title)
        TextView newsTitle;
        @BindView(R.id.news_subtitle)
        TextView newsSubtitle;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
